<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}

$avialable_langs = explode(",", $option['avialable_langs']);
$avialable_langs_desc = explode(",", $option['avialable_langs_desc']);

if (isset($act) && is_numeric($act))
{
	if ($act==1 && isset($lid))
	{
		if (in_array($lid, $avialable_langs) && $option['default_lang']!=$lid)
		{
			$newLangArray=array(); $newLangDescArray=array();
			for($i=0; $i<count($avialable_langs); $i++)
			{
				if ($avialable_langs[$i]!=$lid)
				{
					$newLangArray[$i]=$avialable_langs[$i];
					$newLangDescArray[$i]=$avialable_langs_desc[$i];
				}
			}

			$newLangStr=implode(",", $newLangArray);
			$newLangDescStr=implode(",", $newLangDescArray);

			$queryLangCode="UPDATE ".$PortalPref."options SET option_value = '".$newLangStr."' WHERE option_parametr ='avialable_langs'";
			$queryLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescStr."' WHERE option_parametr ='avialable_langs_desc'";
			$queryDlTble="DROP TABLE ".$PortalPref."langs_".$lid."";

			if (!mysql_query($queryLangCode) || !mysql_query($queryLangDesc))
			{
				mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
				mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
			}else
			{
				if (!mysql_query($queryDlTble))
				{
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
				}
			}
		}
	}

	if ($act==2 && isset($lid))
	{
		if (in_array($lid, $avialable_langs) && $option['default_lang']!=$lid)
		{
			$newDefault=array_search($lid, $avialable_langs);
			$oldDefault=array_search($option['default_lang'], $avialable_langs);

			$newLangsArray=$avialable_langs;
			$newLangsArray[$oldDefault]=$avialable_langs[$newDefault];
			$newLangsArray[$newDefault]=$avialable_langs[$oldDefault];

			$newLangsDescArray=$avialable_langs_desc;
			$newLangsDescArray[$oldDefault]=$avialable_langs_desc[$newDefault];
			$newLangsDescArray[$newDefault]=$avialable_langs_desc[$oldDefault];

			$newLangStr=implode(",", $newLangsArray);
			$newLangDescStr=implode(",", $newLangsDescArray);

			$queryLangCode="UPDATE ".$PortalPref."options SET option_value = '".$newLangStr."' WHERE option_parametr ='avialable_langs'";
			$queryLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescStr."' WHERE option_parametr ='avialable_langs_desc'";
			$queryLangDefault="UPDATE ".$PortalPref."options SET option_value = '".$lid."' WHERE option_parametr ='default_lang'";

			if (!mysql_query($queryLangCode) || !mysql_query($queryLangDesc) || !mysql_query($queryLangDefault))
			{
				mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
				mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
				mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['default_lang']."' WHERE option_parametr ='default_lang'");
			}
		}
	}

	if ($act==3 && isset($lid))
	{
		if (in_array($lid, $avialable_langs) && $option['default_lang']!=$lid)
		{
			$max=count($avialable_langs)-1;
			$forChange=array_search($lid, $avialable_langs);
			$newIndex=$forChange+1;

			if ($newIndex<=$max)
			{
				$newLangsArray=$avialable_langs;
				$newLangsArray[$forChange]=$avialable_langs[$newIndex];
				$newLangsArray[$newIndex]=$avialable_langs[$forChange];

				$newLangsDescArray=$avialable_langs_desc;
				$newLangsDescArray[$forChange]=$avialable_langs_desc[$newIndex];
				$newLangsDescArray[$newIndex]=$avialable_langs_desc[$forChange];

				$newLangStr=implode(",", $newLangsArray);
				$newLangDescStr=implode(",", $newLangsDescArray);

				$queryLangCode="UPDATE ".$PortalPref."options SET option_value = '".$newLangStr."' WHERE option_parametr ='avialable_langs'";
				$queryLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescStr."' WHERE option_parametr ='avialable_langs_desc'";

				if (!mysql_query($queryLangCode) || !mysql_query($queryLangDesc))
				{
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
				}
			}
		}
	}

	if ($act==4 && isset($lid))
	{
		if (in_array($lid, $avialable_langs) && $option['default_lang']!=$lid)
		{
			$forChange=array_search($lid, $avialable_langs);
			$newIndex=$forChange-1;

			if ($newIndex>0)
			{
				$newLangsArray=$avialable_langs;
				$newLangsArray[$forChange]=$avialable_langs[$newIndex];
				$newLangsArray[$newIndex]=$avialable_langs[$forChange];

				$newLangsDescArray=$avialable_langs_desc;
				$newLangsDescArray[$forChange]=$avialable_langs_desc[$newIndex];
				$newLangsDescArray[$newIndex]=$avialable_langs_desc[$forChange];

				$newLangStr=implode(",", $newLangsArray);
				$newLangDescStr=implode(",", $newLangsDescArray);

				$queryLangCode="UPDATE ".$PortalPref."options SET option_value = '".$newLangStr."' WHERE option_parametr ='avialable_langs'";
				$queryLangDesc="UPDATE ".$PortalPref."options SET option_value = '".$newLangDescStr."' WHERE option_parametr ='avialable_langs_desc'";

				if (!mysql_query($queryLangCode) || !mysql_query($queryLangDesc))
				{
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs']."' WHERE option_parametr ='avialable_langs'");
					mysql_query("UPDATE ".$PortalPref."options SET option_value = '".$option['avialable_langs_desc']."' WHERE option_parametr ='avialable_langs_desc'");
				}
			}
		}
	}

	if ($act==5 && isset($lvid) && isset($mod))
	{
		for($li=0; $li<count($avialable_langs); $li++)
		{
			mysql_query("DELETE FROM ".$PortalPref."langs_".$avialable_langs[$li]." WHERE lang_module='".$mod."' AND lang_param='".$lvid."'");
		}
	}
}

echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk."'>";
?>